﻿using System.Collections.Generic;
using System.Linq;
using Super_Management_Mayhem.Content.Spritesheet;
using Transformable_Engine_v2.Engine;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.GameObjects.Containers.Transformable_controls;
using Transformable_Engine_v2.Engine.Time;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Playing.UI
{
    public class FpsDrawer : Container
    {
        private Label _fps;
        private Label _drawFps;

        private List<double> _fpsValues;
        private List<double> _drawFpsValues;

        private const int MAX_VALUES = 1;

        public override void LoadContent()
        {
            _fps = new Label(MenuSheet.GetInstance().GetSpriteAlphabet<MenuSheet.LetterAlphabet>, "")
            {
                PositionGridBound = new TE_Vector2(0, 0),
                TextAlign = TE_Rectangle.Alignment.CenterLeft
            };

            _drawFps = new Label(MenuSheet.GetInstance().GetSpriteAlphabet<MenuSheet.LetterAlphabet>, "")
            {
                PositionGridBound = new TE_Vector2(9,  0),
                TextAlign = TE_Rectangle.Alignment.CenterLeft
            };

            _fpsValues = new List<double>();
            _drawFpsValues = new List<double>();

            AddChildren(_fps, _drawFps);
            base.LoadContent();

            AddTimers(new Timer(100, true, PollFps).AutoStart());
        }

        void PollFps()
        {
            _fpsValues.Add(Game.UpdateFps);
            _drawFpsValues.Add(Game.Fps);

            if (_fpsValues.Count > MAX_VALUES)
                _fpsValues.RemoveAt(0);

            if (_drawFpsValues.Count > MAX_VALUES)
                _drawFpsValues.RemoveAt(0);

            _fps.SetText("Logic fps " + _fpsValues.Average().ToString("0"));
            _drawFps.SetText("Draw fps " + _drawFpsValues.Average().ToString("0"));
        }
    }
}